package com.zqxq.common.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * Copyright(C) 2019  Rights Reserved.
 * <p>
 * FileName: RandomUtil
 * <p>
 * Description:
 * <p>
 * History:
 * 版本号    作者    日期
 * 1.0      sxm    2019/6/11
 */

public class RandomUtil {

    static Set resultSet = null;

    public RandomUtil(){
        resultSet = new HashSet();
    }

    /**
     * 根据目标对象随机取数据
     * @param originList 目标对象
     * @param num 随机个数
     */
    public static Set randomCourseSet(List originList,int num){
        if (ListUtil.isEmpty(originList) || num <= 0) {
            return null;
        }
        Random random = new Random();
        int randomNum = random.nextInt(originList.size());//随机生成数字
        for (int i = 0; i < originList.size(); i++) {
            if (randomNum == i && resultSet.size() < num) {
                resultSet.add(originList.get(i));
            }
        }
        if (resultSet.size() < num) {
            randomCourseSet(originList,num); //递归体
        }
        return resultSet;
    }


}
